<?php

namespace app\admin\controller;

use think\Controller;
use think\facade\Env;
use app\models\{Shoplist, Goods, Cate, Region};
use think\Db;
use oss\Oss;

class Base extends Controller
{


    public function initialize()
    {
        $menu = config('menu.menu');
        $controller = strtolower($this->request->controller());
        $action = strtolower($this->request->action());
        $path = [];
        if (isset($menu[$controller])) {
            $path['c'] = $menu[$controller]['name'] ?: null;
            $path['a'] = $menu[$controller]['sub'][$action]['name'] ?: null;
            $path['s'] = $menu[$controller]['sub'][$action]['show'] ?: null;
        }
        $this->assign('path', $path);
    }

    protected function get_rand_str($len = 6)
    {
        $arr = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'), array('$', '@', '#', '%', '&'));
        shuffle($arr);
        $sub_arr = array_slice($arr, 0, $len);
        return implode('', $sub_arr);
    }

    public function upload_img()
    {
        $file = $_FILES['file'];
        $oss = new Oss();
        $oss->setUploadType('ram');
        $url = $oss->uploadPostFile($file, 'baicao/admin' . date('Y-m-d'));
        return json(['code' => 0, 'status' => 0, 'data' => ['src' => $url]]);
    }

    // public function upload_img()
    // {
    //     if ($this->request->isPost()) {
    //         $file = request()->file('file');
    //         $path =  $_SERVER['DOCUMENT_ROOT'] . "/uploads/";
    //         $info = $file->validate(['size' => 1024 * 1024 * 5, 'ext' => 'jpg,png,gif,jpeg'])
    //             ->move($path);
    //         if (!$info) {
    //             return json(['code' => 1, 'data' => []]);
    //             $this->result([], 1, $file->getError());
    //         } else {
    //             $url = '/uploads' . '/' . date("Ymd") . '/' . $info->getfileName();
    //             $url = url($url, '', '', true);
    //             return json(['code' => 0, 'data' => ['src' => $url]]);
    //         }
    //     }
    // }

    public function gettypes()
    {
        $catemodel = new Cate();
        $goodsmodel = new Goods();
        $type = $this->request->get('type/d', 0);
        $shopid = $this->request->get('shopid/d', 0);
        switch ($type) {
            case '1':
                $result = $goodsmodel->where(['shopid' => $shopid, 'isSale' => 1, 'isdel' => 1])->field('id,title')->select();
                break;
            case '2':
                $result = $catemodel->where(['shopid' => $shopid])->field('id,name as title')->select();
                break;
            default:
                return json(['code' => 1, 'data' => []]);
                break;
        }
        if (count($result) > 0) {
            return json(['code' => 0, 'data' => $result]);
        }
        return json(['code' => 1, 'data' => []]);
    }

    public function shoplist()
    {
        $model = new Shoplist();
        $title = $this->request->get('title', '');
        $result = $model->field('id,title,account')->where('title|account', 'like', '%' . $title . '%')->select();
        if (count($result) > 0) {
            return json(['code' => 0, 'data' => $result]);
        }
        return json(['code' => 1, 'data' => []]);
    }

    public function getaddress()
    {
        $keyword = $this->request->get('keyword/s', ''); //关键词
        $url = 'https://apis.map.qq.com/ws/place/v1/suggestion';
        $result = api_curl($url, ['keyword' => $keyword, 'key' => 'UHRBZ-EYNKG-MKSQO-IEY66-FHOGV-W3FKM']);
        $result = json_decode($result, true);
        if (!empty($result) && $result['status'] == 0 && count($result['data']) > 0) {
            return json(['code' => 0, 'data' => $result['data']]);
        }
        return json(['code' => 1, 'data' => []]);
    }


    public function getarea2()
    {
        $id = $this->request->get('id/d', 0); //区域ID
        $adminid = $this->request->get('adminid/d', 0); //管理员ID
        $level = $this->request->get('level/d', 0); //行政等级
        if ($adminid > 0) {
            $admin = Db::name('admin')->where('id', $adminid)->find();
            $admin['street'] = explode(',', $admin['street']);
            $admin['area'] = explode(',', $admin['area']);
        } else {
            $admin = $this->request->admin; //管理员信息
        }
        if ($id > 0) {
            if ($level == 1) {
                $list = (new Region())->where(['level' => $level])->select();
            }
            if ($level == 2) {
                $parent_id = (new Region())->where(['area_code' => $id, 'level' => ($level - 1)])->value('id');
                $list = (new Region())->where(['parent_id' => $parent_id])->select();
            }
            if ($level == 3) {
                $parent_id = (new Region())->where(['area_code' => $id, 'level' => ($level - 1)])->value('id');
                $list = (new Region())->where(['parent_id' => $parent_id])->select();
            }
            if ($level == 4) {
                $parent_id = (new Region())->where(['area_code' => $id, 'level' => ($level - 1)])->value('id');
                $list = (new Region())->where(['parent_id' => $parent_id])->select();
            }
        } else {
            if ($level == 1) {
                $list = (new Region())->where(['level' => $level])->select();
            }
            if ($level == 2) {
                $parent_id = (new Region())->where(['area_code' => $admin['province'], 'level' => ($level - 1)])->value('id');
                $list = (new Region())->where(['parent_id' => $parent_id])->select();
            }
            if ($level == 3) {
                $parent_id = (new Region())->where(['area_code' => $admin['city'], 'level' => ($level - 1)])->value('id');
                $list = (new Region())->where(['parent_id' => $parent_id])->select();
            }
            if ($level == 4) {
                $parent_id = (new Region())->where(['area_code' => $admin['area'], 'level' => ($level - 1)])->value('id');
                $list = (new Region())->where(['parent_id' => $parent_id])->select();
            }
        }
        return json(['code' => 0, 'status' => 0, 'data' => $list, 'province' => $admin['province'], 'city' => $admin['city'], 'street' => $admin['street'], 'area' => $admin['area'], 'type' => $admin['type']]);
    }
}
